def write_to_file(fname, content):
    try:
        f = open(fname,'w')
        f.write(content)
        f.close()
    except IOError:
        print "no write_to_file (%s)" % content
    finally:
        if f.closed == False:
            f.close()

def utf8_line(x):
    return (x + unichr(10)).encode('utf-8')

def dict_read(filename, fun=lambda x: x):
    if filename == None:
        raise ValueError('dict_read(filename): filename unexpected None')
    result = dict()
    if os.path.exists(filename):
        try:
            f = open(filename, 'r')
            for item in f.read().split('\n'):
                (key, value) = item.split(':')
                result[key] = fun(value)
        except Exception, ex:
            print 'filename %s' % filename
            print repr(ex)
            print 'dict_read(filename, fun): result failure.'
            result = dict()
        finally:
            try:
                f.close()
            except Exception, ex:
                print 'filename %s' % filename
                print repr(ex)
                print 'Nice to know: dict_read(filename, fun) close failure.'
    return result

def list_read(filename, fun=lambda x: x):
    if filename == None:
        raise ValueError('list_read(filename): filename unexpected None')
    result = []
    if os.path.exists(filename):
        try:
            f = open(filename, 'r')
            result = [fun(item) for item in f.read().split('\n') if len(item) > 1]
        except Exception, ex:
            print 'filename %s' % filename
            print repr(ex)
            print 'list_read(filename, fun): result failure.'
            result = []
        finally:
            try:
                f.close()
            except Exception, ex:
                print 'filename %s' % filename
                print repr(ex)
                print 'Nice to know: list_read(filename, fun) close failure.'
    return result

def list_write(filename, value, fun=lambda x: x):
    if filename == None:
        raise ValueError('list_write(filename, value, fun): filename unexpected None')
    if value == None:
        raise ValueError('list_write(filename, value, fun): value unexpected None')
    # pdb.set_trace()
    try:
       f = open(filename, 'w')
    except Exception, ex:
       print repr(ex)
       raise IOError('list_write(filename, value, fun): open(%s, "w") failure.' % filename)
    for item in value:
        try:
           f.write(fun(item))
        except Exception, ex:
           print repr(ex)
           print 'list_write(filename, value, fun): write failure associated to item %s.' % item
    try:
       f.close()
    except Exception, ex:
        print repr(ex)
        print 'list_write(filename, value, fun): just want to know about f.close() failure.'


def open_store(filename):
    if os.path.exists(filename):
	return open(filename, 'a');
    else:
	return open(filename, 'w');

def seq_next(sequence)
    try:
      return sequence.pop(0)
    except Exception:
      return None

import re

def match_or(match, item):
    for element in match:
        match = element.match(item)
        if match != None:
            return True
    return False

def match_and(match, item):
    for element in match:
        match = element.match(item)
        if match == None:
            return False
    return True

